<script type="text/javascript">
	var url;
	var LODOP;
	function newOrderCar(){
		$('#dlg').dialog('open').dialog('setTitle','添加申请单');
		$('#fm').form('clear');
		url = '<?php echo url('ordercar','add')?>';
	}
	
	function editOrderCar(){
		var row = $('#dg').datagrid('getSelected');
		if (row){
			$('#dlg').dialog('open').dialog('setTitle','编辑申请单');
			$('#fm').form('load',row);
			url = '<?php echo url('ordercar','update')?>&id='+row.id;
		}
	}
	
	function saveOrderCar(){
		$('#fm').form('submit',{
			url: url,
			onSubmit: function(){
				return $(this).form('validate');
			},
			success: function(result){
				var result = eval('('+result+')');
				if (result.success){
					$('#dlg').dialog('close');		// close the dialog
					$('#dg').datagrid('reload');	// reload the user data
				} else {
					$.messager.show({
						title: '错误',
						msg: result.msg
					});
				}
			}
		});
	}
	
	function removeOrderCar(){
		var row = $('#dg').datagrid('getSelected');
		if (row){
			$.messager.confirm('确认','一定要取消该申请单吗?',function(r){
				if (r){
					$.post('<?php echo url('ordercar','del')?>',{id:row.id},function(result){
						if (result.success){
							$('#dg').datagrid('reload');	// reload the user data
						} else {
							$.messager.show({	// show error message
								title: '错误',
								msg: result.msg
							});
						}
					},'json');
				}
			});
		}
	}
	
	$(function(){
		$('#dg').datagrid({
			view: detailview,
			detailFormatter:function(index,row){
				return '<div class="ddv"><table>' +
				'<tr><td style="border:0; padding:2px 8px;">用车事由: </td>' +
				'<td style="border:0; padding:2px 8px;">' + row.description + '</td></tr>' +
				'<tr><td style="border:0; padding:2px 8px;">备注: </td>' +
				'<td style="border:0; padding:2px 8px;">' + row.comment + '</td></tr>' +
				'</table></div>';
			},
		});
	});
	
	function doSearchDate(){   
		$('#dg').datagrid('load',{   
			date1: $('#dd1').datebox("getValue"),   
			date2: $('#dd2').datebox("getValue")
		}); 
	}
	
	function doSearchNum(){
		$('#dg').datagrid('load',{
			num: $('#num').val()
		});
	}
	
	function printOrderCar(){
		var row = $('#dg').datagrid('getSelected');
		if (row){
			myPreview(row);
		}
	}
	
	function myPreview(row) {		
		CreatePrintPage(row);
		//LODOP.PRINT_DESIGN();
		LODOP.PREVIEW();
	};
	
	function CreatePrintPage(row) {
		d_arr=row.date.split("-");
		if(row.amount_real==0)row.amount_real='';
		LODOP=getLodop(document.getElementById('LODOP_OB'),document.getElementById('LODOP_EM'));  
		LODOP.PRINT_INIT("用车申请单");
		LODOP.SET_PRINT_PAGESIZE(1,2100,1200,"CreateCustomPage");
		LODOP.SET_PRINT_STYLE("FontColor","#000000");
		LODOP.SET_PRINT_STYLE("FontName","微软雅黑");
		LODOP.SET_PRINT_STYLE("FontSize",12);
		LODOP.SET_PRINT_STYLE("Alignment",2);
		
		LODOP.ADD_PRINT_TEXT(25,20,754,35,"菱塘回族乡乡级领导、政府办公室用车申请单");
		LODOP.SET_PRINT_STYLEA(0,"FontSize",18);

		LODOP.ADD_PRINT_TEXT(65,20,50,25,d_arr[0]);
		LODOP.SET_PRINT_STYLEA(0,"FontSize",10);
		LODOP.SET_PRINT_STYLEA(0,"FontColor","#0000FF");
		LODOP.ADD_PRINT_TEXT(65,70,25,25,"年");
		LODOP.SET_PRINT_STYLEA(0,"FontSize",10);
		LODOP.ADD_PRINT_TEXT(65,95,25,25,d_arr[1]);
		LODOP.SET_PRINT_STYLEA(0,"FontSize",10);
		LODOP.SET_PRINT_STYLEA(0,"FontColor","#0000FF");
		LODOP.ADD_PRINT_TEXT(65,120,25,25,"月");
		LODOP.SET_PRINT_STYLEA(0,"FontSize",10);
		LODOP.ADD_PRINT_TEXT(65,145,25,25,d_arr[2]);
		LODOP.SET_PRINT_STYLEA(0,"FontSize",10);
		LODOP.SET_PRINT_STYLEA(0,"FontColor","#0000FF");
		LODOP.ADD_PRINT_TEXT(65,170,25,25,"日");
		LODOP.SET_PRINT_STYLEA(0,"FontSize",10);

		LODOP.ADD_PRINT_TEXT(65,624,30,25,"№:");
		LODOP.SET_PRINT_STYLEA(0,"FontSize",10);
		LODOP.ADD_PRINT_TEXT(65,654,120,25,row.num);
		LODOP.SET_PRINT_STYLEA(0,"FontSize",10);
		LODOP.SET_PRINT_STYLEA(0,"FontColor","#0000FF");
		LODOP.ADD_PRINT_BARCODE(393,20,50,50,"QRCode",row.num);
		LODOP.SET_PRINT_STYLEA(0,"Color","#0000FF");
		LODOP.SET_PRINT_STYLEA(0,"QRCodeVersion",1);
		LODOP.SET_PRINT_STYLEA(0,"NotOnlyHighPrecision",true);
		
		LODOP.ADD_PRINT_RECT(95,20,754,287,0,1);
		LODOP.ADD_PRINT_RECT(145,20,754,1,0,1);
		LODOP.ADD_PRINT_RECT(195,20,754,1,0,1);
		LODOP.ADD_PRINT_RECT(245,20,754,1,0,1);
		LODOP.ADD_PRINT_RECT(295,20,754,1,0,1);
		LODOP.ADD_PRINT_RECT(95,120,1,200,0,1);
		LODOP.ADD_PRINT_RECT(145,397,1,150,0,1);
		LODOP.ADD_PRINT_RECT(145,497,1,150,0,1);
		
		LODOP.ADD_PRINT_TEXT(110,20,100,30,"用车事由");
		LODOP.ADD_PRINT_TEXT(160,20,100,30,"用车人");
		LODOP.ADD_PRINT_TEXT(210,20,100,30,"随行人员");
		LODOP.ADD_PRINT_TEXT(260,20,100,30,"预估价");
		LODOP.ADD_PRINT_TEXT(160,397,100,30,"车主/车牌号");
		LODOP.ADD_PRINT_TEXT(210,397,100,30,"去向");
		LODOP.ADD_PRINT_TEXT(260,397,100,30,"实际价");
		LODOP.ADD_PRINT_TEXT(305,20,280,30,"备注（超过预估价必须说明情况）：");
		LODOP.ADD_PRINT_TEXT(110,124,650,30,row.description);
		LODOP.SET_PRINT_STYLEA(0,"FontColor","#0000FF");
		LODOP.SET_PRINT_STYLEA(0,"Alignment",1);
		LODOP.ADD_PRINT_TEXT(160,120,277,30,row.uname);
		LODOP.SET_PRINT_STYLEA(0,"FontColor","#0000FF");
		LODOP.ADD_PRINT_TEXT(160,497,277,30,row.driver+"  "+row.car_id);
		LODOP.SET_PRINT_STYLEA(0,"FontColor","#0000FF");
		LODOP.ADD_PRINT_TEXT(210,120,277,30,row.colleague);
		LODOP.SET_PRINT_STYLEA(0,"FontColor","#0000FF");
		LODOP.ADD_PRINT_TEXT(210,497,277,30,row.destination);
		LODOP.SET_PRINT_STYLEA(0,"FontColor","#0000FF");
		LODOP.ADD_PRINT_TEXT(260,120,277,30,row.amount_estimated);
		LODOP.SET_PRINT_STYLEA(0,"FontColor","#0000FF");
		LODOP.ADD_PRINT_TEXT(260,497,277,30,row.amount_real);
		LODOP.SET_PRINT_STYLEA(0,"FontColor","#0000FF");
		LODOP.ADD_PRINT_TEXT(340,24,750,30,row.comment);
		LODOP.SET_PRINT_STYLEA(0,"FontColor","#0000FF");
		LODOP.SET_PRINT_STYLEA(0,"Alignment",1);
		LODOP.ADD_PRINT_TEXT(405,120,100,30,"经办人：");
		LODOP.SET_PRINT_STYLEA(0,"FontSize",10);
		LODOP.SET_PRINT_STYLEA(0,"Alignment",1);
		LODOP.ADD_PRINT_TEXT(405,497,100,30,"审批人：");
		LODOP.SET_PRINT_STYLEA(0,"FontSize",10);
		LODOP.SET_PRINT_STYLEA(0,"Alignment",1);
	};
</script>

<table id="dg" title="用车申请与打印" class="easyui-datagrid"
		url="<?php echo url('ordercar','list')?>"
		toolbar="#toolbar" pagination="true"
		rownumbers="true" fitColumns="true" fit="true" singleSelect="true">
	<thead>
		<tr>
			<th field="num" width="50">编号</th>
			<th field="date" width="50">日期</th>
			<th field="uname" width="50">用车人</th>
			<th field="driver" width="50">车主</th>
			<th field="car_id" width="50">车牌号</th>
			<th field="colleague" width="100">随行人员</th>
			<th field="destination" width="50">去向</th>
			<th field="amount_estimated" width="50">预估价</th>
			<th field="amount_real" width="50">实际价</th>
		</tr>
	</thead>
</table>
<div id="toolbar" style="padding:5px;height:auto">
	<div style="margin-bottom:5px">
		<a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newOrderCar()">添加申请单</a>
		<a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editOrderCar()">编辑申请单</a>
		<a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="removeOrderCar()">取消申请单</a>
		<a href="#" class="easyui-linkbutton" iconCls="icon-print" plain="true" onclick="printOrderCar()">打印申请单</a>
	</div>
	<div>
		<label>按单据编号：</label><input id="num" />
		<a href="#" class="easyui-linkbutton" iconCls="icon-search" plain="true" onclick="doSearchNum()">搜索</a>
	</div>
	<div>
		<label>按日期范围：</label><input id="dd1" name="dd1" class="easyui-datebox" /><label>至</label><input id="dd2" name="dd2" class="easyui-datebox" />
		<a href="#" class="easyui-linkbutton" iconCls="icon-search" plain="true" onclick="doSearchDate()">搜索</a>
	</div>
</div>

<div id="dlg" class="easyui-dialog" style="width:400px;height:480px;padding:10px 20px"
		closed="true" buttons="#dlg-buttons">
	<div class="ftitle">申请单信息</div>
	<form id="fm" method="post" novalidate>
		<div class="fitem">
			<label>用车人:</label>
			<select class="easyui-combotree" url="<?php echo url('ordercar','combobox')?>" name="uid" style="width:156px;"/>
			<input name="uname" type="hidden">
		</div>
		<div class="fitem">
			<label>车主:</label>
			<input name="driver" class="easyui-validatebox" required="true">
		</div>
		<div class="fitem">
			<label>车牌号:</label>
			<input name="car_id" class="easyui-validatebox">
		</div>
		<div class="fitem">
			<label>去向:</label>
			<input name="destination" class="easyui-validatebox" required="true">
		</div>
		<div class="fitem">
			<label>预估价:</label>
			<input name="amount_estimated" class="easyui-validatebox" required="true">
		</div>
		<div class="fitem">
			<label>实际价:</label>
			<input name="amount_real" class="easyui-validatebox">
		</div>
		<div class="fitem">
			<label>用车事由:</label>
			<textarea name="description" class="easyui-validatebox" required="true"></textarea>
		</div>
		<div class="fitem">
			<label>随行人员:</label>
			<textarea name="colleague" class="easyui-validatebox"></textarea>
		</div>
		<div class="fitem">
			<label>备注:</label>
			<textarea name="comment" class="easyui-validatebox"></textarea>
		</div>
	</form>
</div>
<div id="dlg-buttons">
	<a href="#" class="easyui-linkbutton" iconCls="icon-ok" onclick="saveOrderCar()">保存</a>
	<a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')">取消</a>
</div>